/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.util  
 */
package com.apache.design.util;

import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;

/**
 * description:  
 */
public class GZipUtil {

	/**
	 * 
	 * @Title: pack
	 * @Description: 将一组文件打成tar包
	 * @param sources 要打包的原文件数组
	 * @param target 打包后的文件
	 * @return File   返回打包后的文件
	 * @throws
	 */
	public static File pack(File[] sources, File target) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(target);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		TarArchiveOutputStream os = new TarArchiveOutputStream(out);
		for (File file : sources) {
			try {
				os.putArchiveEntry(new TarArchiveEntry(file));
				IOUtils.copy(new FileInputStream(file), os);
				os.closeArchiveEntry();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (os != null) {
			try {
				os.flush();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return target;
	}

	public static void zip(String inputFileName, String zipFileName) throws Exception {
		zip(zipFileName, new File(inputFileName));
	}

	private static void zip(String zipFileName, File inputFile) throws Exception {
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
		String sysType = ConfigUtil.getSystemType();
		if ("windows".equalsIgnoreCase(sysType)) {
			out.setEncoding(System.getProperty("sun.jnu.encoding"));
		} else {
			out.setEncoding("UTF-8");// 解决linux乱码 根据linux系统的实际编码设置,可以使用命令   
			// vi/etc/sysconfig/i18n 查看linux的系统编码   
		}
		zip(out, inputFile, "", sysType);
		out.close();
	}

	private static void zip(ZipOutputStream out, File f, String base, String sysType) throws Exception {
		if (f.isDirectory()) { //判断是否为目录   
			File[] fl = f.listFiles();
			// out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));   
			// out.putNextEntry(new ZipEntry(base + "/"));   
			ZipEntry zipEntry = new ZipEntry(base + System.getProperties().getProperty("file.separator"));
			if (!"windows".equalsIgnoreCase(sysType)) {
				zipEntry.setUnixMode(755);// 解决linux乱码   
			}
			out.putNextEntry(zipEntry);
			// base = base.length() == 0 ? "" : base + "/";   
			base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");
			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + fl[i].getName(), sysType);
			}
		} else { // 压缩目录中的所有文件   
					// out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));   
			ZipEntry zipEntry = new ZipEntry(base);
			if (!"windows".equalsIgnoreCase(sysType)) {
				zipEntry.setUnixMode(644);// 解决linux乱码   
			}
			out.putNextEntry(zipEntry);
			FileInputStream in = new FileInputStream(f);
			int b;
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			in.close();
		}
	}

	/**
	 * 首字母大写
	 * @param str
	 * @return
	 */
	public static String toUpperFirst(String str) {
		if (StrUtil.isNull(str))
			return "";
		String names[] = str.split("_");
		StringBuffer sb = new StringBuffer();
		if (names.length < 2) {
			sb.append(names[0].substring(0, 1).toUpperCase() + names[0].substring(1));
		} else {
			for (int i = 0; i < names.length; i++) {
				sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1));
			}
		}
		return sb.toString();
	}

	/**
	 * 首字母小写
	 * @param str
	 * @return
	 */
	public static String toLowerFirst(String str) {
		if (StrUtil.isNull(str))
			return "";
		StringBuffer sb = new StringBuffer();
		sb.append(str.substring(0, 1).toLowerCase() + str.substring(1));
		return sb.toString();
	}

	/**
	 * 属性值生成,符合骆驼命名
	 * @param str
	 * @return
	 */
	public static String toCamelNamed(String str) {
		if (StrUtil.isNull(str))
			return "";
		String names[] = str.split("_");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < names.length; i++) {
			if (i == 0) {
				sb.append(names[i].toLowerCase());
			} else {
				sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1).toLowerCase());
			}
		}
		return sb.toString();
	}
}
